package com.xzy.rabbit.demo.exchange.c;

import com.rabbitmq.client.Connection;
import com.xzy.rabbit.common.util.RabbitUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Topic交换机（主题模式）
 *
 * @author xzy
 * @date 2021/9/2710:39
 */
class Main {

    /*
     * 模拟一个招聘信息发布与订阅的场景
     *
     * 招聘信息的标识：<City>.<Work>.<Salary>.<Keyword>
     */

    private static final String EXCHANGE_NAME = "xzy.exchange.topic";
    private static final Integer MESSAGE_COUNT = 3;
    private static final long WORK_TIME = 3000L;
    private static final String[] EXCHANGE_ROUTING_KEY = {
            "杭州.Java.13-15k.996",
            "杭州.C++.15-20k.007",
            "杭州.Python.15-20k.996",
            "上海.Java.15-18k.996",
            "北京.Java.16-19k.996",
            "深圳.Java.17-20k.996",
    };

    private static final String QUEUE_NAME_1 = "xzy.queue.topic1";
    private static final String QUEUE_NAME_2 = "xzy.queue.topic2";
    private static final String[] QUEUE_ROUTING_KEY_1 = {"杭州.Java.#","#.Java.16-19k.#"};
    private static final String[] QUEUE_ROUTING_KEY_2 = {"杭州.#","#.Java.#"};


    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.获取连接
        Connection connection = RabbitUtil.getConnection();

        // 2.发布消息（发布招聘信息）
        Thread producer = new Thread(new Producer(connection, EXCHANGE_NAME, EXCHANGE_ROUTING_KEY, MESSAGE_COUNT, WORK_TIME), "PRODUCER");

        // 2.订阅消息（接收招聘信息）
        Thread consumer1 = new Thread(new Consumer(connection, EXCHANGE_NAME, QUEUE_NAME_1, QUEUE_ROUTING_KEY_1), "CONSUMER1");
        Thread consumer2 = new Thread(new Consumer(connection, EXCHANGE_NAME, QUEUE_NAME_2, QUEUE_ROUTING_KEY_2), "CONSUMER2");

        producer.start();
        consumer1.start();
        consumer2.start();

        producer.join();
        consumer1.join();
        consumer2.join();
    }
}
